<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>multi_match 查询 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-match-query.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-match-query.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/multi-match-query.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/multi-match-query.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="multi-field-search.html">多字段搜索</a></span>
»
<span class="breadcrumb-node">multi_match 查询</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_tuning_best_fields_queries.html">« 最佳字段查询调优</a>
</span>
<span class="next">
<a href="most-fields.html">多数字段 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="multi-match-query"></a>multi_match 查询<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/25_Multi_match_query.asciidoc">edit</a>
</h2>
</div></div></div>
<p><code class="literal">multi_match</code> 查询为能在多个字段上反复执行相同查询提供了一种便捷方式。</p>
<div class="note admon">
<div class="icon"></div>
<div class="admon_content">
<p><code class="literal">multi_match</code> 多匹配查询的类型有多种，其中的三种恰巧与 <a class="xref" href="_single_query_string.html#know-your-data" title="了解我们的数据">了解我们的数据</a> 中介绍的三个场景对应，即： <code class="literal">best_fields</code> 、 <code class="literal">most_fields</code> 和 <code class="literal">cross_fields</code> （最佳字段、多数字段、跨字段）。</p>
</div>
</div>
<p>默认情况下，查询的类型是 <code class="literal">best_fields</code> ，这表示它会为每个字段生成一个 <code class="literal">match</code> 查询，然后将它们组合到 <code class="literal">dis_max</code> 查询的内部，如下：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "dis_max": {
    "queries":  [
      {
        "match": {
          "title": {
            "query": "Quick brown fox",
            "minimum_should_match": "30%"
          }
        }
      },
      {
        "match": {
          "body": {
            "query": "Quick brown fox",
            "minimum_should_match": "30%"
          }
        }
      },
    ],
    "tie_breaker": 0.3
  }
}</pre>
</div>
<p>上面这个查询用 <code class="literal">multi_match</code> 重写成更简洁的形式：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">{
    "multi_match": {
        "query":                "Quick brown fox",
        "type":                 "best_fields", <a id="CO69-1"></a><i class="conum" data-value="1"></i>
        "fields":               [ "title", "body" ],
        "tie_breaker":          0.3,
        "minimum_should_match": "30%" <a id="CO69-2"></a><i class="conum" data-value="2"></i>
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/110_Multi_Field_Search/25_Best_fields.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO69-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">best_fields</code> 类型是默认值，可以不指定。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO69-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>如 <code class="literal">minimum_should_match</code> 或 <code class="literal">operator</code> 这样的参数会被传递到生成的 <code class="literal">match</code> 查询中。</p>
</td>
</tr>
</table>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_查询字段名称的模糊匹配"></a>查询字段名称的模糊匹配<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/25_Multi_match_query.asciidoc">edit</a>
</h3>
</div></div></div>
<p>字段名称可以用模糊匹配的方式给出：任何与模糊模式正则匹配的字段都会被包括在搜索条件中，例如可以使用以下方式同时匹配 <code class="literal">book_title</code> 、 <code class="literal">chapter_title</code> 和 <code class="literal">section_title</code> （书名、章名、节名）这三个字段：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "multi_match": {
        "query":  "Quick brown fox",
        "fields": "*_title"
    }
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_提升单个字段的权重"></a>提升单个字段的权重<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/110_Multi_Field_Search/25_Multi_match_query.asciidoc">edit</a>
</h3>
</div></div></div>
<p>可以使用 <code class="literal">^</code> 字符语法为单个字段提升权重，在字段名称的末尾添加 <code class="literal">^boost</code> ，其中 <code class="literal">boost</code> 是一个浮点数：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "multi_match": {
        "query":  "Quick brown fox",
        "fields": [ "*_title", "chapter_title^2" ] <a id="CO70-1"></a><i class="conum" data-value="1"></i>
    }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO70-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">chapter_title</code> 这个字段的 <code class="literal">boost</code> 值为 <code class="literal">2</code> ，而其他两个字段 <code class="literal">book_title</code> 和 <code class="literal">section_title</code> 字段的默认 boost 值为 <code class="literal">1</code> 。</p>
</td>
</tr>
</table>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="_tuning_best_fields_queries.html">« 最佳字段查询调优</a>
</span>
<span class="next">
<a href="most-fields.html">多数字段 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>